# -*- coding: utf8 -*-
# $Id :  $



# Copyright 2008-2010, Pascal Fautrero & JCharles
#
# This file is part of PyArchiver.
# 
# PyArchiver is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyArchiver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyArchiver.  If not, see <http://www.gnu.org/licenses/>.



# Import packages


import sys
import os
import pygame.mixer
import wx                        # This module uses the new wx namespace
import main_frame
import wx.lib.throbber
import version_info
import throb_images
import thread
import time

#import images                   # For PyEmbeddedImage

_ = wx.GetTranslation            # Define _ or add _ to builtins in your app file

appDir = os.path.split(os.path.abspath(sys.argv[0]))[0]

#---------------------------------------------------------------------------

class My_SplashScreenCustom(wx.Frame):
    def __init__(self, parent, id=-1, title="SplashScreen",
                 style=wx.FRAME_SHAPED | wx.SIMPLE_BORDER | 
                 wx.STAY_ON_TOP | wx.FRAME_NO_TASKBAR,
                 duration=2000, bitmap="",   # 1500
                 pos=wx.DefaultPosition):
        
        #--------------

        bitmapDir = os.path.join(appDir, "bitmaps")
        self.bmp = wx.Bitmap(os.path.join(bitmapDir, "splashScreen.png"))
        # or PyEmbeddedImage
        # self.bmp = images.SplashScreen.GetBitmap()
        size = (self.bmp.GetWidth(), self.bmp.GetHeight())

        self.images = [throb_images.catalog[img].getBitmap()
                       for img in throb_images.index
                       if img not in ["eclouds", "logo"]]
       
        #-------------------------------------------------------------------

        wx.Frame.__init__(self, parent, id, title, pos, size, style)

        #-------------------------------------------------------------------

        self.duration = duration        
        self.hasShaped = False

        #--------------
        
        dc = wx.ClientDC(self)
        dc.DrawBitmap(self.bmp, 0, 0, True)
        self.set_windows_shape()

        #--------------

        self.throb = wx.lib.throbber.Throbber(self, -1,
                                              self.images,
                                              label="",
                                              size=(34, 34),
                                              pos=(400, 270),##########
                                              frameDelay=0.1)
        
        #--------------
        
        # Bind some events to an events handler
        self.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_click)
        self.Bind(wx.EVT_PAINT, self.on_paint)
        self.Bind(wx.EVT_WINDOW_CREATE, self.set_windows_shape)
        self.Bind(wx.EVT_CLOSE, self.on_close)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.on_erase_background)
        
        #--------------
        
        self.SetTransparent(230)

        #--------------
        
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.time_out)        

        #--------------

        self.fc = wx.FutureCall(3000, self.show_main)  # 3000
        
        #-------------------------------------------------------------------
        
        # Simplified init method
        self.do_layout()
        self.play()
        
        #-------------------------------------------------------------------
        
        self.CentreOnScreen(wx.BOTH)
        self.throb.Start()
        
    #-----------------------------------------------------------------------

    def do_layout(self):
        if wx.Platform == "__WXMAC__":
            borderSizer = wx.BoxSizer(wx.HORIZONTAL)
            borderSizer.Add(self.throb, 0, wx.ALIGN_RIGHT | wx.ALL, 20)

            allSizer = wx.BoxSizer(wx.VERTICAL)
            allSizer.Add(borderSizer, 0, wx.ALIGN_RIGHT, 0)

            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(allSizer, 1, wx.EXPAND | wx.TOP, 200)

            self.SetAutoLayout(True)
#           self.SetSizer(sizer)
#           sizer.Fit(panel)
            sizer.Fit(self)  

        else:
            borderSizer = wx.BoxSizer(wx.HORIZONTAL)
            allSizer = wx.BoxSizer(wx.VERTICAL)
            sizer = wx.BoxSizer(wx.VERTICAL)

            #--------------

            # wx.BoxSizer(window, proportion, flag, border)        
            # wx.BoxSizer(sizer, proportion, flag, border)         
            borderSizer.Add(self.throb, 0, wx.LEFT, 373)
            allSizer.Add(borderSizer, 1, wx.ALIGN_RIGHT | wx.ALL, 32)
            sizer.Add(allSizer, 0, wx.EXPAND | wx.TOP, 237)
        
            #--------------
        
            self.SetAutoLayout(True)
            self.SetSizer(sizer)
            sizer.Fit(self)

        
    def play(self):
        pygame.mixer.init(22050,-16,2,2048)

        music_Location = os.path.join(appDir, "music")
        music_File = os.path.join(music_Location, "startup.ogg")
        # print "Path sound : %s" % (music_Location)
        
        pygame.mixer.music.load(music_File)
        print ""
        print "Play sound : %s" % (music_File)
        pygame.mixer.music.play()
        # pygame.mixer.music.fadeout(300)
        # pygame.mixer.music.stop()
        

    def my_worker(self):
        time.sleep(10)       
        wx.CallAfter(self.throb.Stop)

        
    def on_start(self):
        thread.start_new(self.my_worker, ())
        

    def on_close(self, event):
        # Make sure the default handler runs too so
        # this window gets destroyed
        event.Skip()
        self.Hide()
     
        # If the timer is still running then go
        # ahead and show the main frame now
        if self.fc.IsRunning():
            self.fc.Stop()
            self.show_main()

            
    def on_erase_background(self, event):
        dc = event.GetDC()
        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)


    def on_mouse_click(self, event):
        self.timer.Notify()
        
     
    def time_out(self, event):
        self.Close(True)

        
    def set_windows_shape(self, event=None):
        region = wx.RegionFromBitmap(self.bmp)
        self.hasShaped = self.SetShape(region)

        
    def on_paint(self, event):
        dc = wx.PaintDC(self)
        dc.DrawBitmap(self.bmp, 0, 0, True)
        
        self.timer.Start(self.duration, True)
            
        #--------------
            
        fontSize = self.GetFont().GetPointSize()
        
        # wx.Font(pointSize, family, style, weight, underline, faceName)
        if wx.Platform == "__WXMSW__":
            self.normalBoldFont = wx.Font(fontSize+2,
                                          wx.DEFAULT, wx.NORMAL, wx.BOLD, False, "")
            self.normalFont = wx.Font(fontSize+2,
                                      wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, "")
            self.chgtFont = wx.Font(fontSize,
                                    wx.DEFAULT, wx.NORMAL, wx.NORMAL, True, "")

            dc.SetFont(self.chgtFont)
            dc.SetFont(self.normalFont)
            dc.SetFont(self.normalBoldFont)
            
        else:
            self.normalBoldFont = wx.Font(fontSize-1,
                                          wx.DEFAULT, wx.NORMAL, wx.BOLD, False, "")
            self.normalFont = wx.Font(fontSize-1,
                                      wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, "")
            self.chgtFont = wx.Font(fontSize-2,
                                    wx.DEFAULT, wx.NORMAL, wx.NORMAL, True, "")
              
            dc.SetFont(self.chgtFont)
            dc.SetFont(self.normalFont)
            dc.SetFont(self.normalBoldFont)
        
        #--------------

        dc.SetTextForeground(wx.WHITE)        
        dc.SetFont(self.normalBoldFont)
        dc.DrawText(_("Version %s") % version_info.APPLICATIONVERSION_STRING, x=350, y=130)        
        dc.SetFont(self.normalBoldFont)
        dc.DrawText(_("License granted to :"), x=50, y=130) 
        dc.SetFont(self.normalFont)
        dc.DrawText("%s" % version_info.ENTERPRISELICENSE_STRING, x=50, y=145)
        dc.SetTextForeground(wx.BLACK)
        dc.SetFont(self.chgtFont)
        dc.DrawText(_("Loading..."), x=385, y=310)
        dc.SetTextForeground(wx.WHITE)
        dc.DrawText(_("Loading..."), x=386, y=311)

        
    def show_main(self):
        self.frame = main_frame.My_Frame(None, -1, _("PyArchiver v%s")
                                         % version_info.APPLICATIONVERSION_STRING)

        iconDir = os.path.join(appDir, "icons")
        frameicon = wx.Icon(os.path.join(iconDir, "icon_App.ico"), wx.BITMAP_TYPE_ICO)
        # or PyEmbeddedImage
        # frameicon = images.Printing.GetIcon() 
        self.frame.SetIcon(frameicon)
            
        self.frame.Show()
     
        if self.fc.IsRunning():
            self.Raise()



